#include "apue.h"
#include <setjmp.h>

#define TOK_ADD 5

jmp_buf jmpbuffer;

void do_line(char*);

void cmd_add(void);

int get_token(void);

int main(void)
{
    char line[MAXLINE];

    if (setjmp(jmpbuffer) != 0)
        printf("double setJmp cause error\n");

    printf("Jmp\n");
    while (fgets(line, MAXLINE, stdin) != NULL)
        do_line(line);
    exit(0);
}

char* tok_ptr;

void do_line(char* ptr)
{
    int cmd;
    tok_ptr = ptr;
    while ((cmd = get_token()) > 0)
    {
        switch (cmd)
        {
        case TOK_ADD:
            cmd_add();
            break;
        }
    }
}

void cmd_add(void)
{
    int token;

    token = get_token();

    if (token == TOK_ADD)
        longjmp(jmpbuffer, 1);
}

int get_token(void)
{
    printf("cmd = %s\n", tok_ptr);
    if (atoi(tok_ptr) < 0)
        return -1;

    return TOK_ADD;
}
